<?php

use Illuminate\Database\Seeder;
use Carbon\Carbon;
use App\Models\Record;
use App\Models\User;
use App\Models\Business;

class RecordTableSeeder extends Seeder
{
    public $evaluation_results = array('优秀','良好','一般','关注','重点关注');
    public $department_ids = array(7,8,9,10,11,12,13, 14);
    public $faker;
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        $this->faker = app(Faker\Generator::class);
        $businesses = Business::all();
        $now = Carbon::now();
        foreach ($businesses as $business){
            if ($business->is_statistical) { //开始填充数据
                switch ($business->data_cycle) {
                    case '月':
                        for ($month = 1; $month < $now->month; $month++) {
                            $record_date = Carbon::create(2019,$month)->lastOfMonth();
                            foreach ($this->department_ids as $department_id){
                                $record = $this->creatNewRecord($business->id,$department_id,$record_date);
                                $this->addResponsibility($department_id,$business,$record);
                            }
                        }
                        break;
                    case '季度':
                        for ($quarter = 1; $quarter < $now->quarter; $quarter++) {
                            $record_date = Carbon::create(2019, $quarter * 3)->lastOfMonth();
                            foreach ($this->department_ids as $department_id){
                                $record = $this->creatNewRecord($business->id,$department_id,$record_date);
                                $this->addResponsibility($department_id,$business,$record);
                            }
                        }
                        break;
                    case '半年':
                        $record_date = $now;
                        if ($now->month > 6) {
                            $record_date = Carbon::create($now->year, 6)->lastOfMonth();
                        }else{
                            $record_date = Carbon::create($now->year - 1, 12)->lastOfMonth();
                        }
                        //半年的数据模拟五次
                        for ($index  = 1; $index  <= 5; $index++) {
                            foreach ($this->department_ids as $department_id){
                                $record = $this->creatNewRecord($business->id,$department_id,$record_date);
                                $this->addResponsibility($department_id,$business,$record);
                            }
                            $record_date = $record_date->subMonth(6);
                        }
                        break;
                    case '年':
                        $record_date = Carbon::create($now->year - 1,12)->lastOfMonth() ;
                        for ($index = 1; $index <= 5; $index++) {
                            foreach ($this->department_ids as $department_id){
                                $record = $this->creatNewRecord($business->id,$department_id,$record_date);
                                $this->addResponsibility($department_id,$business,$record);
                            }
                            $record_date = $record_date->subYear(1)->lastOfMonth();
                        }
                        break;
                }
            }

        }
    }
    public function creatNewRecord(int $businessId,int $departmentId,Carbon $recordDate){
        $record = Record::create([
            'business_id' => $businessId,
            'department_id' => $departmentId,
            'value' => random_int(100,10000),
            'score' => random_int(0, 100),
            'evaluation_result' => $this->evaluation_results[random_int(0, 4)],
            'comment' => $this->faker->sentence,
            'is_evaluated_by_hand' => false,
            'record_date' => $recordDate->format('Y-m-d'),
        ]);
        return $record;
    }
    public function addResponsibility(int $department_id,Business $business,Record $record){
        $users = User::where('department_id','=',$department_id)->get();
        $roleName = $business->name.'责任人';
        foreach ($users as $user) {
            if($user->isRole($roleName)){
                $record->users()->attach($user->id);
            }
        }
    }
}
